'use strict';

const Service = require('egg').Service;

class UploadService extends Service {
  constructor(ctx) {
    super(ctx);
    this.name = 'upload';
  }

  async create(params) {
    const { mysql } = this.app;
    try {
      const { name, url, description, type } = params;
      const createTime = new Date();
      const result = await mysql.query(
        "INSERT INTO sys_file(id,  name,  url,  description,  type, created_time) VALUES (REPLACE(UUID(),'-',''), ?, ?, ?, ?, ?);",
        [ name, url, description, type, createTime ]
      );
      if (result.affectedRows === 1) {
        return result.insertId;
      }
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }

  async destroy(params) {
    const { mysql } = this.app;
    try {
      const { id } = params;
      const result = await mysql.delete('cms_category', { category_id: id });
      if (result.affectedRows === 1) {
        return {};
      }
    } catch (error) {
      this.ctx.throw(500, error.message);
    }
  }
}

module.exports = UploadService;
